﻿using BrnMall.Core;
using BrnMall.Manager.Domain.AddressBook;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BrnMall.Manager.Manager.AddressBook
{
    /// <summary>
    /// 部门成员管理
    /// </summary>
    public class StaffManager
    {
        static string TableName = "crm_staff";

        public static bool Add(Staff model)
        {
            using (var db = new QDMallDatabase())
            {
                db.Insert(model);
            }
            return model.Id > 0;
        }

        public static int Add2(Staff model)
        {
            object result = RDBSHelper.ExecuteScalar(CommandType.Text, string.Format("insert into {0}(storeid,uid,name,mobile,phone,position,Department) values({1},{2},'{3}','{4}','{5}','{6}','{7}');select @@identity",
                    TableName, model.StoreId, model.Uid, model.Name, model.Mobile, model.Phone, model.Position, model.Department));
            return Convert.ToInt32(result);
        }

        public static Staff Get(int id)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<Staff>(string.Format("select top 1* from {1} where id={0}", id, TableName));
            }
        }

        public static Staff GetByUid(int uid)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<Staff>(string.Format("select top 1 * from {1} where uid={0}", uid, TableName));
            }
        }

        public static Staff GetByWxUserId(int storeId,string wxUserId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<Staff>(string.Format("select top 1* from {1} where storeId={0} and wxUserId='{2}'", storeId, TableName, wxUserId));
            }
        }

        public static List<Staff> GetListByIds(string ids)
        {
            using (var db = new QDMallDatabase())
            {
                if (ids.Contains(","))
                    return db.Fetch<Staff>(string.Format("select * from {1} where id in({0})", ids, TableName));
                else
                    return db.Fetch<Staff>(string.Format("select * from {1} where id={0}", ids, TableName));
            }
        }

        public static List<Staff> GetListByStoreId(int storeId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<Staff>(string.Format("select * from {1} where storeid={0}", storeId, TableName));
            }
        }


        //public static List<Staff> GetListByDepartmentIds(string ids)
        //{
        //    using (var db = new QDMallDatabase())
        //    {
        //        if (ids.Contains(","))
        //            return db.Fetch<Staff>(string.Format("select * from {1} where DepartmentId in({0})", ids, TableName));
        //        else
        //            return db.Fetch<Staff>(string.Format("select * from {1} where DepartmentId={0}", ids, TableName));
        //    }
        //}

        /// <summary>
        /// 成员修改资料
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool UpdateByStaff(Staff model)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count = db.Execute($"update {TableName} set Avater=@1,Wxid=@2,Phone=@3,Mobile=@4,Email=@5,Regionid=@6,Address=@7 where id=@0",
                    model.Id,
                    model.Avater,
                    model.Wxid,
                    model.Phone,
                    model.Mobile,
                    model.Email,
                    model.Regionid,
                    model.Address);
            }
            return count > 0;
        }

        /// <summary>
        /// 修改个性签名
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sign"></param>
        /// <returns></returns>
        public static bool UpdpateSign(int id, string sign)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count = db.Execute($"update {TableName} set sign=@1 where id=@0", id, sign);
            }
            return count > 0;
        }

        /// <summary>
        /// 修改手机号
        /// </summary>
        /// <param name="id"></param>
        /// <param name="mobile"></param>
        /// <param name="mobileState"></param>
        /// <returns></returns>
        public static bool UpdateMobile(int id, string mobile, int mobileState)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count = db.Execute($"update {TableName} set mobile=@1,mobileState=@2 where id=@0", id, mobile, mobileState);
            }
            return count > 0;
        }

        /// <summary>
        /// 更相名片浏览数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool UpdateClickCount(int id)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count = db.Execute($"update {TableName} set ClickCount=isnull(ClickCount,0)+1  where id={id}");
            }
            return count > 0;
        }
        /// <summary>
        /// 点赞数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool UpdateUpCount(int id)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count = db.Execute($"update {TableName} set upCount=isnull(upCount,0)+1  where id={id}");
            }
            return count > 0;
        }
        /// <summary>
        /// 个性签名点赞数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool UpdateSignUpCount(int id)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count = db.Execute($"update {TableName} set SignUpCount=isnull(SignUpCount,0)+1  where id={id}");
            }
            return count > 0;
        }

        /// <summary>
        /// 名片转发数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool UpdateShareCount(int id)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count = db.Execute($"update {TableName} set shareCount=isnull(shareCount,0)+1  where id={id}");
            }
            return count > 0;
        }

        /// <summary>
        /// /更改名片状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="aiState">名片状态（0 未屏蔽,1 已屏蔽名片）</param>
        /// <returns></returns>
        public static bool UpdateCardState(int id,int cardState)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count = db.Execute($"update {TableName} set cardState={cardState} where id={id}");
            }
            return count > 0;
        }

        /// <summary>
        /// Boss雷达状态
        /// </summary>
        public static bool UpdateBossState(int id, int state)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Execute($"update {TableName} set bossState={state} where id={id}") > 0;
            }
        }

        /// <summary>
        /// AI雷达状态（0未开通，1开通）
        /// </summary>
        public static bool UpdateAiState(int id, int state)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Execute($"update {TableName} set aiState={state} where id={id}") > 0;
            }
        }

        /// <summary>
        /// 修改名片二维码
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sign"></param>
        /// <returns></returns>
        public static bool UpdpateQrCode(int id, string qrCode)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count = db.Execute($"update {TableName} set qrCode=@1 where id=@0", id, qrCode);
            }
            return count > 0;
        }

        public static int Update(Staff model)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Update(model);
            }
        }

        //public static int Update(Staff model)
        //{
        //    var sql = new StringBuilder();
        //    sql.AppendFormat("update {0} set ", TableName);
        //    if (!string.IsNullOrEmpty(model.Avater))
        //        sql.AppendFormat("avater='{0}',", model.Avater);
        //    if (!string.IsNullOrEmpty(model.Name))
        //        sql.AppendFormat("name='{0}',", model.Name);
        //    if (!string.IsNullOrEmpty(model.Position))
        //        sql.AppendFormat("position='{0}',", model.Position);
        //    if (!string.IsNullOrEmpty(model.Mobile))
        //        sql.AppendFormat("mobile='{0}',", model.Mobile);
        //    if (!string.IsNullOrEmpty(model.Email))
        //        sql.AppendFormat("email='{0}',", model.Email);
        //    if (!string.IsNullOrEmpty(model.Department))
        //        sql.AppendFormat("department='{0}',", model.Department);
        //    sql.AppendFormat("storeid={0} where Id={1}", model.StoreId, model.Id);
        //    return RDBSHelper.ExecuteNonQuery(CommandType.Text, sql.ToString());
        //}

        /// <summary>
        /// 删除所有选中项
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool DeleteAll(string id)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Execute($"delete from {TableName} where id in ({id})") > 0;
            }
        }

        /// <summary>
        /// 根据部门查找对应的人员
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static List<Staff> GetDepartmentById(int StoreId, int page, int pageSize)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<Staff>(page, pageSize, string.Format("select * from {0} where StoreId={1}", TableName, StoreId));
            }
        }

        public static List<Staff> GetDepartmentByName(string name, int page, int pageSize)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<Staff>(page, pageSize, string.Format("select * from {0} where Name like '%{1}%'", TableName, name));
            }
        }

        /// <summary>
        /// 获取总记录数
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static int GetRecordCount(int StoreId, string Nickname)
        {
            var sql = new StringBuilder();
            sql.AppendFormat("select count(1) from {0} where storeid={1}", TableName, StoreId);
            if (string.IsNullOrEmpty(Nickname))
                sql.AppendFormat(" and Name like '%{0}%'", Nickname);
            using (var db = new QDMallDatabase())
            {
                return db.ExecuteScalar<int>(sql.ToString());
            }
        }
    }
}